package com.guoguo.service.impl;

import com.github.pagehelper.PageHelper;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Favorite;
import com.guoguo.entity.UserExt;
import com.guoguo.entity.vo.ImagesVO;
import com.guoguo.entity.vo.ModelAlbumVO;
import com.guoguo.entity.vo.UserExtVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.mapper.FavoriteMapper;
import com.guoguo.service.ModelAlbumService;
import com.guoguo.mapper.ModelMapper;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Service
public class ModelAlbumServiceImpl implements ModelAlbumService {
    // ModelMapper接口
    @Autowired
    private ModelMapper modelAlbumMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    public PageBean<ModelAlbumVO> getModelAlbumList(String modelId, String userId) {
        PageHelper.startPage(1, 9);
        List<ModelAlbumVO> modelAlbumVOs = modelAlbumMapper.getModelAlbumList(modelId);
//        List<AliOssImageConfig> aliOssImageConfigsList = new ArrayList<>();
//        aliOssImageConfigsList.add(new AliOssImageConfig("test"));
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (ModelAlbumVO modelAlbumVO : modelAlbumVOs) {
            List<ImagesVO> imagesVOs = modelAlbumVO.getImagesVOs();
            for (ImagesVO imagesVO : imagesVOs) {
                List<String> files = new ArrayList<>();
                files.add(imagesVO.getOriginalUrl());
                files.add(imagesVO.getThumbnailsUrl());
                Map<String, String> urlMap = AliOssUtils.getImageUrlList(files);//, aliOssImageConfigsList);
                imagesVO.setOriginalUrl(urlMap.get(imagesVO.getOriginalUrl()));
                imagesVO.setThumbnailsUrl(urlMap.get(imagesVO.getThumbnailsUrl()));

            }
            Favorite favorite = new Favorite();
            favorite.setModelId(modelId);
            favorite.setUserId(userId);
            //收藏关注
            List<Favorite> favorites = favoriteMapper.getFavoriteByIds(favorite);
            modelAlbumVO.setArgs2(favorites.size() > 0 ? true : false);
            favorite.setAlbId(modelAlbumVO.getAlbId());
            //用户关注头像
            List<UserExtVO> userExtVOs = modelAlbumMapper.getFavoriteUserIcons(favorite);//modelAlbumVO.getUserExtVOs();
            for (UserExtVO userExtVO : userExtVOs) {
                userExtVO.setIcon(AliOssUtils.getImageUrl(userExtVO.getIcon()));
            }
            String icon = AliOssUtils.getImageUrl(modelAlbumVO.getModelIcon());//, aliOssImageConfig);
            modelAlbumVO.setModelIcon(icon);
            modelAlbumVO.setUserExtVOs(userExtVOs);
        }

        return new PageBean<ModelAlbumVO>(modelAlbumVOs);
    }
}
